package com.evangelion.aciton;

import com.evangelion.lock.EvangelionvrLock;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@RestController
public class EvangelionvrAction {
    private volatile Integer stock = 5;
    private EvangelionvrLock lock = new EvangelionvrLock();

    private ReentrantLock reentrantLock = new ReentrantLock();

    @RequestMapping("/execute")
    public  String execute() {
        //lock.lock();

        reentrantLock.lock();
        if(stock > 0){
            stock = stock - 1;
            System.out.println("-----------下单成功，剩余库存为："+stock);
        }else{
            System.out.println("-----------下单失败，已售罄!");
        }
        reentrantLock.lock();
        return "123";
    }

    public String getOrderNumber() {
        Lock lock = new ReentrantLock();
        lock.lock();
        try {
            return getOrderNumber_Q();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return null;
    }

    public String getOrderNumber_Q() {
        return "\t 生成订单号：" + (++num);
    }


    private static int num = 0;
}
